/*
 * Copyright 2012 Clement Sannicolo <clement.sannicolo@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Ext.define('App.VTypes',{});

Ext.apply(Ext.form.field.VTypes,
{
	// Name
	name: function(value, field)
	{
		return (value.length > 2 && value.length <= 255);
	},
	nameText: 'This field should be alphanum from 3 to 255 characters',
	
	// IconCls
	iconCls: function(value, field)
	{
		return /^icon-[a-z0-9\-]{1,50}$/.test(value);
	},
	iconClsText: 'This field should be alphanum lowercase from 6 to 50 characters',
	iconClsMask: /^[a-z0-9\-]$/,
	
	// Password
	password: function(value, field)
	{
		return /^[a-z0-9_]{6,12}$/i.test(value);
	},
	passwordText: 'This field should be alphanum from 6 to 12 characters',
	passwordMask: /^[a-z0-9_]$/i,

	// Numeric
	numeric: function(value, field)
	{
		return /^[\d]+\.?[\d]*$/.test(value);
	},
	numericText: 'This field should be numeric',
	numericMask: /^[\d\.]$/,
	
	// Integer
	integer: function(value, field)
	{
		return /^[\d]+$/.test(value);
	},
	integerText: 'This field should be integer',
	integerMask: /^[\d]$/,

	// Daterange
	daterange: function(value, field)
	{
		var date = field.parseDate(value);
		if (!date) {
			return false;
		}
		if (field.startDateField && (!this.dateRangeMax || date.getTime() != this.dateRangeMax.getTime())) {
			var start = field.up('form').down('#' + field.startDateField);
			start.setMaxValue(date);
			start.validate();
			this.dateRangeMax = date;
		}
		else if (field.endDateField && (!this.dateRangeMin || date.getTime() != this.dateRangeMin.getTime())) {
			var end = field.up('form').down('#' + field.endDateField);
			end.setMinValue(date);
			end.validate();
			this.dateRangeMin = date;
		}
	return true;
	},
	daterangeText: 'Start date must be less than end date',
	
	// Time
	time: function(value, field)
	{
		return /^([0-1][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/.test(value);
	},
	timeText: 'This field should be like 00:00:00',
	timeMask: /^[\d:]$/
});